<%@page import="com.wanmait.cookbook.vo.Userinfo"%>
<%@page import="com.wanmait.cookbook.vo.Pager"%>
<%@page import="com.wanmait.cookbook.vo.Cookbook"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%String path = request.getContextPath(); %>
<jsp:include page="/manage/inc/head.jsp"></jsp:include>
<jsp:include page="/manage/inc/menu.jsp"></jsp:include>
<!--/sidebar-->
<div class="main-wrap">

	<div class="crumb-wrap">
		<div class="crumb-list">
			<i class="icon-font"></i><a href="<%=path%>/manage/index.jsp">首页</a><span
				class="crumb-step">&gt;</span><span class="crumb-name">用户管理</span>
		</div>
	</div>
	<div class="search-wrap">
		<div class="search-content">
			<form action="<%=path %>/UserinfoManageServlet?action=findUserinfoByInput" id="myform" method="post">
				<table class="search-tab">
					<tr>
						<th width="70">关键字:</th>
						<td><input class="common-text" placeholder="请输入用户名"
							name="val" id="find" type="text"></td>
						<td><input class="btn btn-primary btn2" id="findbut" name="sub" value="查询"
							type="button"><span style="color:red" id="findspan"></span></td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	<div class="result-wrap">

		<form name="myform" id="myform1" method="post">
			<div class="result-title">
				<div class="result-list">
				</div>
			</div>
			<div class="result-content">
				<table class="result-tab" width="100%">
					<tr>
						<th class="tc" width="5%"><input class="allChoose" name=""
							type="checkbox"></th>
						<th>序号</th>
						<th>设置热门</th>
						<th>用户名</th>
						<th>头像</th>
						<th>食谱发布量</th>
						<th>作品发布量</th>
						<th>注册时间</th>
						<th>禁言状态</th>
						<th>封号状态</th>
					</tr>
					<%
							
                        List<Userinfo> userinfos =(List<Userinfo>)request.getAttribute("userinfos");
                        List<Integer> cookbookCounts =(List<Integer>)request.getAttribute("cookbookCounts");
                        List<Integer> worksCounts =(List<Integer>)request.getAttribute("worksCounts");
                        for(int i=0;i<userinfos.size();i++) {	
                     %>
					<tr id="tr<%=userinfos.get(i).getId()%>">
						<td class="tc"><input data-id="<%=userinfos.get(i).getId() %>" class="choose" name="id[]"
							type="checkbox"></td>
						<td><%=i+1 %></td>
						<td><select class="sethot" 
							data-id="<%=userinfos.get(i).getId() %>" name="hot">
								<%if(userinfos.get(i).getHot()) {%>
								<option value="1">热门</option>
								<option value="0">不热门</option>
								<%} else{%>
								<option value="0">不热门</option>
								<option value="1">热门</option>
								<%} %>
						</select></td>
						<td><%=userinfos.get(i).getUsername() %></td>
						<td><img
							src="<%=path%>/static/image/<%=userinfos.get(i).getAvatar() %>"
							width="50" height="50"></td>
						<td><%=cookbookCounts.get(i) %></td>
						<td><%=worksCounts.get(i) %></td>
						<td><%=userinfos.get(i).getInputTime() %></td>
						<td>
							<select class="setmute" data-id="<%=userinfos.get(i).getId() %>" >
							<%if(userinfos.get(i).getMute()) {%>
								<option value="1" selected>已禁言</option>
								<option value="0">未禁言</option>
							<%}else{ %>
								<option value="1">已禁言</option>
								<option value="0" selected>未禁言</option>
							<%} %>
							</select></td>
						<td>
							<select class="setban" data-id="<%=userinfos.get(i).getId() %>" >
							<%if(userinfos.get(i).getLoginProhibited()) {%>
								<option value="1" selected>已封号</option>
								<option value="0">未封号</option>
							<%}else{ %>
								<option value="1">已封号</option>
								<option value="0" selected>未封号</option>
							<%} %>
							</select></td>
					</tr>
					<%
                        	}
                        %>
				</table>
					<div class="list-page">
					<!-- 上一页 -->
					<a id="prev" href="javascript:void(0);">上一页</a>
							<!-- 显示数字 -->
							<%Pager pager = (Pager)request.getAttribute("pager"); %>
							<%
							int start = 1;
							int end = pager.getPageCount();
							if(pager.getPageCount()>5)
							{
								start = pager.getPageNo()-2<1?1:pager.getPageNo()-2;
								end = start+4;
								if(end>pager.getPageCount())
								{
									end = pager.getPageCount();
									start = end-4;
								}
							}
							for(int i=start;i<=end;i++)
							{
							%>
								<a class="number" href="javascript:void(0);"><%=i %></a>
							<% 
							}
							%>
							<!-- 显示数字的结束 -->
							<!-- 下一页 -->
					<a id="next" href="javascript:void(0);">下一页</a>
					共<%=pager.getPageCount() %>页
				</div>
					<script src="<%=request.getContextPath()%>/manage/js/jquery.min.js"></script>
					<script type="text/javascript">
					$(function(){
						//下一页
						$("#next").click(function(){	 
							 	var n =<%=pager.getPageNo()%>
							 	if(n!=<%=pager.getPageCount()%>){
								 $.ajax({
									 url:"<%=request.getContextPath()%>/UserinfoManageServlet?action=getUserinfoPager",
										type:"post",
										data:{pageNo:n+1},
										success:function(mes){
							
											$(".result-content").html(mes);
										}
									});//ajax的结束
							 	}
						});
					
					});
					</script>
			</div>
		</form>
	</div>
</div>
<!--/main-->
<jsp:include page="/manage/inc/foot.jsp"></jsp:include>
<script>

 $(function(){
	
		//设置热门
		$(".result-content").on("change",".sethot",function(){
		  var val = $(this).val();
		var id = $(this).data("id"); 
		  $.ajax({
			url:"<%=path%>/UserinfoManageServlet?action=setUserinfoHot",
			data:{id:id,val:val},
			success:function(mes){
				
			}
		});  
	}); 
	 
	//全选与反选
		$(".result-content").on("click",".allChoose",function(){
		//$(".allChoose").click(function(){
			$(".choose").prop("checked",$(".allChoose").prop("checked"));
		});
	
		//分页
		$(".result-content").on("click",".number",function(){
			//点击数字的时候执行
			//点击的数字变红
			$(".number").css("color","black");//所有数字变成黑色
			$(this).css("color","red");//点击的数字变成红色
			
			//获得点击的数字  $(this)是点击的a
			var n = $(this).html();
			//content是点击的a中间的内容  点击数字
			//从数据库中间查出新的一页的数据
			//Ajax
			$.ajax({
				url:"<%=request.getContextPath()%>/UserinfoManageServlet?action=getUserinfoPager",
				type:"post",
				data:{pageNo:n},
				success:function(mes){
	
					$(".result-content").html(mes);
				}
			});//ajax的结束
			
		});//click事件的结束
		
		//搜索框不能为空
		$("#findbut").click(function(){
			var val =$("#find").val();
			if(val==""){
				$("#findspan").html("不能为空");
			}else{
				$("#findspan").html("");
				$("#myform").submit();
			}
		});
		

		//设置禁言
		$(".result-content").on("change",".setmute",function(){
		  var val = $(this).val();
		var id = $(this).data("id"); 
		  $.ajax({
			url:"<%=path%>/UserinfoManageServlet?action=setUserinfoMute",
			data:{id:id,val:val},
			success:function(mes){
				
			}
		});  
	}); 
		
		
		//设置封号
		$(".result-content").on("change",".setban",function(){
		  var val = $(this).val();
		var id = $(this).data("id"); 
		  $.ajax({
			url:"<%=path%>/UserinfoManageServlet?action=setUserinfoBan",
			data:{id:id,val:val},
			success:function(mes){
				
			}
		});  
	}); 
		
		
	});
</script>